package com.zq.ht.web.controller;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * 在线编辑器获取图片
 * 没使用
 * 保存文件不在项目路径下使用 （如：d:/attached/）
 * 此项目只对.do拦截，进入不了controller
 * @version
 */
//@Controller
public class AttachedController {
	private static final Logger logger = LogManager.getLogger(AttachedController.class);  
    
    @RequestMapping("/attached/{fileType}/{uploadDate}/{fileName}.{suffix}")  
    public void attached(HttpServletRequest request, HttpServletResponse response,   
            @PathVariable String fileType,  
            @PathVariable String uploadDate,  
            @PathVariable String suffix,  
            @PathVariable String fileName) {  
          
        InputStream is = null;  
        OutputStream os = null;
        
        try {  
            File file = new File("d:/attached/" + fileType + "/" + uploadDate + "/" + fileName + "." + suffix);  
            is = new FileInputStream(file);  
            byte[] buffer = new byte[is.available()];
            is.read(buffer);  
              
            os = new BufferedOutputStream(response.getOutputStream());  
            os.write(buffer);  
            os.flush();  
        } catch (Exception e) {  
        	logger.error("读取文件失败", e);  
        } finally {  
            if (is != null) {  
                try {  
                    is.close();  
                } catch (IOException e) {  
                	logger.error("读取文件失败", e);  
                }  
                if (os != null) {  
                    try {  
                        os.close();  
                    } catch (IOException e) {  
                    	logger.error("读取文件失败", e);  
                    }  
                }  
            }  
        }  
    }  
    
}
